<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名" style="z-index:1">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Row>
                                <Col span="18">
                                    <Input v-model="form.validcode" placeholder="请输入验证码"></Input>
                                </Col>
                                <Col span="5">
                                    <img :src="image" alt="验证码" height="30" style="cursor: pointer;" title="看不清可单击图片刷新" @click="getValidateCode"></img>
                                </Col>
                                
                            </Row>
                            
                        </FormItem>
                        <div style="margin-bottom:10px">
                            <Row>
                                <Col span="8">
                                    <Checkbox v-model="form.remember">记住我</Checkbox>
                                </Col>
                                <Col span="16">
                                    <p>忘记密码，点击<a href="#/index/pwdback">这里</a>找回！</p>
                                </Col>
                            </Row>
                        </div>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import util from '../libs/util';

export default {
    data () {
        
        return {
            image:'',
            form: {
                userName: '',
                password: '',
                validcode: '',
                remember: true
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ],
                validcode: [
                    { required: true, message: '验证码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        getValidateCode(){
            this.api.post("member/validate_code",{},res=>{
                this.image=res.value;
            })
        },
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.api.post("member/login", {username: this.form.userName, password: this.form.password,validate_code: this.form.validcode }, res => {
                        if(!res.value) { this.$CustomMessage.error(res.message); this.getValidateCode(); return; }
                        if(this.form.remember){
                            Cookies.set('membername',this.form.userName);
                            Cookies.set('memberpass',this.form.password);
                        }else{
                            Cookies.remove('membername');
                            Cookies.remove('memberpass');
                        }
                        this.$store.commit('login', {
                            username: this.form.userName,
                            password: this.form.password,
                            access: 'member',
                            token: res.message,
                        });
                        window.location.href = util.getRuntimeUrl('member', 'home');
                    });
                }
            });
        }
    },
    created(){
        this.form.userName=Cookies.get('membername');
        this.form.password=Cookies.get('memberpass');
        if(Cookies.get('access') == "member") {
            this.$router.push({ name: "index" });
        }
        this.api.post("member/validate_code",{},res=>{
            this.image=res.value;
        }) 
    }
};
</script>
